<template>
	<view class="container">
		<cu-custom bgColor="bg-gradual-orange" :isBack="true">
			<block slot="content">核销优惠券</block>
		</cu-custom>
		<!-- 	<view class="header">
			<text class="title">核销优惠券</text>
		</view> -->

		<view class="content">
			<view class="input-group">
				<view class="label">
					优惠券码：
				</view>
				<input v-model="couponCode" type="text" placeholder="请输入优惠券码" />

				<!-- <input class="input" type="text" v-model="couponCode" placeholder="请输入优惠券码"
					placeholder-class="placeholder" confirm-type="done" maxlength="50" /> -->
			</view>

			<view class="button-group">
				<button class="button" @click="verifyCoupon">手动核销</button>
				<button class="button scan-button" @click="scanCode">扫码核销</button>
			</view>
		</view>

		<view class="footer">
			<text class="footer-text">请确保优惠券码正确</text>
		</view>
	</view>
</template>

<script>
	import shop from '@/api/shop.js';
	export default {
		data() {
			return {
				couponCode: ''
			};
		},
		methods: {
			scanCode() {
				uni.scanCode({
					success: (res) => {
						this.couponCode = res.result;
						this.verifyCouponAPI(this.couponCode);
					},
					fail: () => {
						uni.showToast({
							title: '扫码失败，请重试',
							icon: 'none'
						});
					}
				});
			},
			verifyCoupon() {
				if (!this.couponCode) {
					uni.showToast({
						title: '请输入优惠券码',
						icon: 'none'
					});
					return;
				}
				this.verifyCouponAPI(this.couponCode);
			},
			verifyCouponAPI(couponCode) {

				shop.verifyCoupon({couponCode:couponCode}).then(res => {
					if (res.code === 1) {
						uni.showToast({
							title: '核销成功',
							icon: 'success'
						});
						this.couponCode = '';
					} else {
						uni.showToast({
							title: '核销失败：' + res.msg,
							icon: 'none'
						});
					}
				})

			}
		}
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		/* padding: 32rpx; */
		background-color: #f2f4f6;
		box-sizing: border-box;
	}

	.header {
		/* margin-bottom: 48rpx; */
		/* text-align: center; */
	}

	.title {
		font-size: 36rpx;
		font-weight: 600;
		color: #1a1a1a;
	}

	.content {
		padding: 32rpx;
		flex: 1;
		display: flex;
		flex-direction: column;
		/* justify-content: center; */
	}

	.input-group {
		display: flex;
		flex-direction: column;
		background-color: #fff;
		border-radius: 12rpx;
		padding: 24rpx;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.05);
		margin-bottom: 32rpx;
	}

	.label {
		font-size: 38rpx;
		color: #333;
		margin-bottom: 26rpx;
	}

	.input {
		width: 100%;
		font-size: 30rpx;
		padding: 24rpx 20rpx;
		border: 1rpx solid #ddd;
		border-radius: 10rpx;
		background-color: #fafafa;
		box-sizing: border-box;
	}

	.input:focus {
		border-color: #007aff;
		background-color: #fff;
	}

	.placeholder {
		color: #bbb;
	}

	.button-group {
		display: flex;
		justify-content: space-between;
		gap: 20rpx;
		flex-wrap: wrap;
		margin-top: 10rpx;
	}

	.button {
		flex: 1;
		text-align: center;
		padding: 24rpx;
		font-size: 30rpx;
		font-weight: bold;
		border-radius: 12rpx;
		background-color: #007aff;
		color: #fff;
		box-shadow: 0 6rpx 12rpx rgba(0, 122, 255, 0.3);
		transition: background-color 0.3s ease;
	}

	.button:active {
		background-color: #005fcc;
	}

	.scan-button {
		background-color: #28a745;
		box-shadow: 0 6rpx 12rpx rgba(40, 167, 69, 0.3);
	}

	.footer {
		text-align: center;
		margin-top: 40rpx;
	}

	.footer-text {
		font-size: 26rpx;
		color: #999;
	}
</style>